package org.best.practices.android.rotationasynctask.activity;

import org.best.practices.android.R;
import org.best.practices.android.rotationasynctask.task.LongOperationAsyncTask;
import org.best.practices.android.rotationasynctask.task.OnProgressOperationListener;

import com.google.inject.Inject;

import roboguice.activity.RoboActivity;
import roboguice.inject.ContentView;
import roboguice.inject.InjectView;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

@ContentView(R.layout.activity_main)
public class MainActivity extends RoboActivity implements OnProgressOperationListener {
    @SuppressWarnings("unused")
    private final static String TAG = "MainActivity";
    
    private LongOperationAsyncTask mTask;

    private @Inject Handler mHandler;
    
    private @InjectView(R.id.run_info_text) TextView mMainText;
    private @InjectView(R.id.run_longoperation_progress) Button mProgressButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // method is now depracated
        Object retainedTask = getLastNonConfigurationInstance();
        if (retainedTask instanceof LongOperationAsyncTask) {
            mTask = (LongOperationAsyncTask) retainedTask;
        } else {
            mTask = new LongOperationAsyncTask();
        }
        mTask.setOnProgressListener(this);
        
        mProgressButton.setOnClickListener(new ProgressButtonListener());
    }

    @Override
    public Object onRetainNonConfigurationInstance() {
        mTask.removeOnProgressListener();    
        return mTask;
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    private class ProgressButtonListener implements OnClickListener {
        public void onClick(View v) {
            // start progress operation async task
            if (mTask.getStatus().equals(AsyncTask.Status.FINISHED)) {
                mTask = new LongOperationAsyncTask();
                mTask.execute();
            } else {
                mMainText.append("Long Operation Task already started!\n");
            }
        }
    }
    
    // this method might be called from another thread
    @Override
    public void onProgressOperationStep(final int completed, final int total) {
        mHandler.post(new Runnable() {
            public void run() {
                mMainText.append(String.format("Operation in progress: %d / %d\n", completed, total));
            }
        });
    }

    // this method might be called from another thread
    @Override
    public void onProgressOperationFinished() {
        mHandler.post(new Runnable() {
            public void run() {
                mMainText.append("Progress Operation Finished\n");
            }
        });
    }
}
